package app.fugema.shop.main.order.adapter;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.base.common.app.AppManager;
import com.base.common.app.LoginUtils;
import com.base.common.netBeanPackage.BaseApiResult;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.DialogUtils;
import com.base.common.utils.LogUtil;
import com.base.common.utils.OnClickCheckedUtil;
import com.base.common.utils.UIUtils;
import com.base.common.view.adapter.ada.BaseRVAdapter;
import com.base.common.view.adapter.ada.SingleRecyclerAdapter;
import com.base.common.view.base.BaseActivity;
import com.base.common.viewmodel.BaseViewObserver;
import com.jeremyliao.liveeventbus.LiveEventBus;

import android.app.Activity;
import android.view.View;

import androidx.lifecycle.LifecycleOwner;
import app.fugema.shop.R;
import app.fugema.shop.constants.AppConstants;
import app.fugema.shop.databinding.OrderItemFunButtonBinding;
import app.fugema.shop.helper.ShareDialogFragment;
import app.fugema.shop.main.evaluate.view.EvaluateAddActivity;
import app.fugema.shop.main.order.model.OrderFunBtn;
import app.fugema.shop.main.order.view.DeliveryCheckDetailActivity;
import app.fugema.shop.main.order.view.OrderAfterSaleSelectActivity;
import app.fugema.shop.main.order.view.OrderDetailActivity;
import app.fugema.shop.main.order.vm.OrderListVM;
import app.fugema.shop.main.wallet.model.BuyBean;
import app.fugema.shop.main.wallet.view.WalletPayDialogFragment;

public class OrderFunButtonAdapter extends SingleRecyclerAdapter<OrderFunBtn, OrderItemFunButtonBinding> {

    private OrderListVM orderListVM;

    public OrderFunButtonAdapter(LifecycleOwner lifecycleOwner, OrderListVM orderListVM) {
        super(lifecycleOwner);
        this.orderListVM = orderListVM;
    }

    public OrderListVM getOrderListVM() {
        return orderListVM;
    }

    //status  1:待付款 2:待发货 3:待收货    4:   5:待评价/评价
    public static List<OrderFunBtn> getStatesButton(int orderStatus,boolean isCanRefund )
    {
        List<OrderFunBtn> btnList;
        LogUtil.e("OrderInfo","OrderStatus = "+orderStatus +" status=" );
        switch (orderStatus)
        {
            //btn 代号 0 查看详情    1取消订单   2退款   3确认收货   4去支付  5我要晒单 6 评价 7查看物流
            case 1:
                btnList = getStatusButton(1,4);
                break;
            case 2:
                btnList = getStatusButton(5,0);
                break;
            case 3:
                btnList = getStatusButton(5,0,3);
                break;
            case 4:
            case 5:
                btnList = getStatusButton(5,6);
                break;
            case 6:
                btnList = getStatusButton(0);
                break;
            default:
                btnList = getStatusButton(0);
                break;
        }
        // 订单 待发货 状态 显示 退款      待收货 待评价 显示 申请售后 即退货退款
        if( isCanRefund && orderStatus == 2 )
        {
            btnList.add( createAdInfo(2, "退款") );
        }
        return btnList;
    }

    private static List<OrderFunBtn> getStatusButton(int... states)
    {
        List<OrderFunBtn> list = new ArrayList<>();
        //所有订单都包含 查看详细
        for (int state : states)
        {
            switch (state)
            {

                case 0:
                    list.add(createAdInfo(0, "查看详情"));
                    break;
                case 1:
                    list.add(createAdInfo(1, "取消订单"));
                    break;
                case 2:
                    list.add(createAdInfo(2, "退款"));
                    break;
                case 3:
                    list.add(createAdInfo(3, "确认收货"));
                    break;
                case 4:
                    list.add(createAdInfo(4, "去支付"));
                    break;
                case 5:
                    list.add(createAdInfo(5, "去晒单"));
                    break;
                case 6:
                    list.add(createAdInfo(6, "评价"));
                    break;
                case 7:
                    list.add(createAdInfo(7, "查看物流"));
                    break;
                case 8:
                    list.add(createAdInfo(8, "追加评论"));
                    break;
                //如果需要多的按钮 在这里添加
               /*
                case 8:
                    list.add(createAdInfo(8, "提醒发货"));
                    break;
                */
            }
        }
        return list;
    }

    // 获取订单详情页   下面的按钮
    //status  1:待付款 2:待发货 3:待收货    4:   5:待评价/评价
    public static List<OrderFunBtn> getDetailStatesButton(int orderStatus,boolean isCanRefund )
    {
        List<OrderFunBtn> btnList;
        LogUtil.e("OrderInfo","OrderStatus = "+orderStatus +" status=" );
        switch (orderStatus)
        {
            //btn 代号 0 查看详情    1取消订单   2退款   3确认收货   4去支付  5我要晒单 6 评价 7查看物流
            case 1:
                btnList = getStatusButton(1,4);
                break;
            case 2:
                btnList = getStatusButton(5);
                break;
            case 3:
                btnList = getStatusButton(5,3,7);
                break;
            case 4:
            case 5:
                btnList = getStatusButton(5,6);
                break;
            //预留
            case 6:
                btnList = getStatusButton(0);
                break;
            default:
                btnList = getStatusButton(0);
                break;
        }
        // 订单 待发货 状态 显示 退款      待收货 待评价 显示 申请售后 即退货退款
        if( isCanRefund && orderStatus == 2 )
        {
            btnList.add( createAdInfo(2, "退款") );
        }
        return btnList;
    }


    @Override
    public void onItemClick(View view, BaseRVAdapter adapter, int onclickType, int position, OrderFunBtn bean)
    {
        super.onItemClick(view, adapter, onclickType, position, bean);
        String userKey = LoginUtils.getToken();
        //0 查看详情    1取消订单   2退款   3确认收货   4去支付 5我要晒单 6 评价 7查看物流
        switch (bean.getTypeId())
        {
            case 0:
                OrderDetailActivity.start(view.getContext(), Long.parseLong(bean.getContent()));
                break;
            case 1:
                // 取消订单
                closeOrder(Long.parseLong(bean.getContent()), userKey);
                break;
            case 2:
                refundOrder(bean.getContent());
                // 仅退款  (待发货 状态)
                OrderAfterSaleSelectActivity.start(view.getContext() , bean.getContent() ,"");
                break;
            case 3:
                //确认收货
                orderReceipt(Long.parseLong(bean.getContent()), userKey);
                break;
            case 4:
                //付款
                Activity activity = AppManager.getInstance().getTopActivity();
                if (activity instanceof BaseActivity)
                {
                    BaseActivity baseActivity = (BaseActivity) activity;
                    BuyBean buyBean = new BuyBean();
                    //钱
                    buyBean.setMoney( BigDecimal.valueOf(bean.getTotalAmount()));
                    buyBean.setOrderNumbers(bean.getContent());
                    baseActivity.addFragment(WalletPayDialogFragment.newInstance(buyBean));
                }
                break;
            // 预留功能
            // 晒单
            case 5:
                shareOrder(bean.getContent());
            break;
            case 6:
                pingJiaOrder(bean.getContent());
                break;
            case 7:
            // 物流信息
            DeliveryCheckDetailActivity.start(view.getContext(), bean.getContent());
            break;
            case 8:
                //追加评论
                break;
        }

    }
    //定义按钮 类型   0 查看详情    1取消订单   2退款   3确认收货   4去支付 5我要晒单 6 评价 7查看物流
    private static OrderFunBtn createAdInfo(int typeId, String text)
    {
        OrderFunBtn btnInfo = new OrderFunBtn();
        btnInfo.setTypeId(typeId);
        btnInfo.setBtnTitle(text);
        switch (typeId)
        {
            case 3:
            case 4:
            case 5:
            case 6:
            case 8:
                //白字
                btnInfo.setSelected(true);
                //红底
                btnInfo.setChecked(true);
                break;
        }
        return btnInfo;
    }


    @Override
    public int getLayoutId()
    {
        return R.layout.order_item_fun_button;
    }


    //晒单
    private void shareOrder(String orderNumber)
    {
        StringBuffer urlSb = new StringBuffer();
        urlSb.append("https://www.fugema.cn/m-weixin/order/ordershare?orderids=");
        urlSb.append(orderNumber);

        BaseActivity baseActivity = (BaseActivity)AppManager.getInstance().getTopActivity();
        baseActivity.addFragment(ShareDialogFragment.shareUrl(baseActivity ,"好商品，要分享",orderNumber,  urlSb.toString(),"",null));
    }

    //退款
    private void refundOrder(String orderNumber)
    {
        //UIUtils.showToastSafes("退款");

    }
    //确认收货
    private void orderReceipt(long orderId,String userKey)
    {
        DialogUtils.showSimpleDialog(AppManager.getInstance().getTopActivity(),false,(dialog, which) -> {

            //UIUtils.showToastSafes("确认收货");
            getOrderListVM().confirmReceiptOrder(orderId,userKey)
                    .observe(getLifecycleOwner(), new BaseViewObserver<LiveDataWrapper<BaseApiResult>>(AppManager.getInstance().getTopActivity())
                    {
                        @Override
                        protected void onSuccess(LiveDataWrapper<BaseApiResult> data)
                        {
                            UIUtils.showToastSafes("确认收货成功");
                            LiveEventBus.get(AppConstants.EventKey.ORDER_RECEIPT, String.class).post(String.valueOf(orderId));
                        }
                    });
        },"确定收到商品？"  );

    }


    //取消订单
    private void closeOrder(long orderId,String userKey )
    {
        if (AppManager.getInstance().getTopActivity() instanceof BaseActivity)
        {
            BaseActivity activity = (BaseActivity) AppManager.getInstance().getTopActivity();
            DialogUtils.showSimpleDialog(activity,false,(dialog, which) -> {

                getOrderListVM().closeOrder(orderId,userKey)
                                .observe(getLifecycleOwner(), new BaseViewObserver<LiveDataWrapper<BaseApiResult>>(AppManager.getInstance().getTopActivity())
                                {
                                    @Override
                                    protected void onSuccess(LiveDataWrapper<BaseApiResult> data)
                                    {
                                        UIUtils.showToastSafes("订单已关闭");
                                        LiveEventBus.get(AppConstants.EventKey.ORDER_CANCEL, String.class).post(""+orderId);
                                    }
                                });
                dialog.dismiss();
            } ,"确定要取消订单？");

        }
    }


    //评价订单
    private void pingJiaOrder(String orderNumber)
    {
        //UIUtils.showToastSafes("评价");
        if (AppManager.getInstance().getTopActivity() instanceof BaseActivity)
        {
            BaseActivity activity = (BaseActivity) AppManager.getInstance().getTopActivity();
            //activity.addFragment(Fragment.getInstanc);
            EvaluateAddActivity.start(activity, Long.parseLong( orderNumber ));
        }
    }

}
